package la.bbs558.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.dev33.satoken.exception.SaTokenException;
import la.bbs558.aop.valid.ValidException;
import la.bbs558.controller.base.CodeEnum;
import la.bbs558.controller.base.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ValidationException;

/**
 * @author: WeiHanmin
 * @description: 全局异常拦截器
 * @time: 2021-09-13 09:51:59
 */
@RestController
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @Resource
    private HttpServletRequest request;

    private void errorPrint(Exception exception) {
        log.error("捕获到异常",exception);
        log.error("uri ===> {} | class ==> {} | message == > {}", request.getRequestURI() ,exception.getClass(), exception.getMessage());
    }

    @ExceptionHandler(ValidException.class)
    public Result<?> validException(ValidException exception) {
        errorPrint(exception);
        return Result.error(exception.getLocalizedMessage());
    }
    @ExceptionHandler(SaTokenException.class)
    public Result<?> saTokenException(SaTokenException exception) {
        errorPrint(exception);
        return Result.error(exception.getLocalizedMessage(), CodeEnum.SA_TOKEN);
    }
    @ExceptionHandler(NotLoginException.class)
    public Result<?> notLoginException(NotLoginException exception) {
        errorPrint(exception);
        return Result.error(exception.getLocalizedMessage(), CodeEnum.NOT_LOGIN);
    }

    @ExceptionHandler(NotRoleException.class)
    public Result<?> notRoleException(NotRoleException exception) {
        errorPrint(exception);
        return Result.error(exception.getLocalizedMessage(), CodeEnum.NOT_AUTH);
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result<?> httpMessageNotReadableException(HttpMessageNotReadableException exception) {
        errorPrint(exception);
        return Result.error(exception.getLocalizedMessage(), CodeEnum.PARAMS);
    }

    @ExceptionHandler(BusinessException.class)
    public Result<?> businessException(BusinessException exception) {
        errorPrint(exception);
        return Result.error(exception.getMessage());
    }

    @ExceptionHandler(MsgException.class)
    public Result<?> msgException(MsgException exception) {
        errorPrint(exception);
        return new Result<>(exception.getMessage(), exception.getLocalizedMessage(), exception.getCodeEnum().getCode());
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public Result<?> illegalArgumentException(IllegalArgumentException exception) {
        errorPrint(exception);
        return Result.error(exception.getMessage());
    }

    @ExceptionHandler(ValidationException.class)
    public Result<?> validationException(ValidationException exception) {
        errorPrint(exception);
        return new Result<>(exception.getMessage(), exception.getLocalizedMessage(), CodeEnum.PARAMS.getCode());
    }

    @ExceptionHandler(Exception.class)
    public Result<?> otherExceptionHandle(Exception exception) {
        errorPrint(exception);
        return Result.error(exception.getMessage(),CodeEnum.SYSTEM);
    }

}
